package com.starryx.sim.mvc.reflect;


import com.esotericsoftware.reflectasm.MethodAccess;
import com.starryx.sim.mvc.annotation.RequestParam;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;


public class MethodInvoker {

	private final MethodAccess invoker;

	private final Object target;

	private final int methodIndex;

	public MethodInvoker(MethodAccess invoker, Object target, int methodIndex) {
		this.invoker = invoker;
		this.target = target;
		this.methodIndex = methodIndex;
	}

	public static MethodInvoker create(Object target, Method method) {
		MethodAccess methodAccess = MethodAccess.get(target.getClass());
		int methodIndex = methodAccess.getIndex(method.getName(), method.getParameterTypes());
		return new MethodInvoker(methodAccess, target, methodIndex);
	}

	public Object invoke(Object... args) {
		return this.invoker.invoke(this.target, this.methodIndex, args);
	}

	public String nane() {
		return this.invoker.getMethodNames()[0];
	}

}
